package 第十七章_XML与JSON.JSON_GSON通用版;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class Json_Gson_test {
    public static void main(String[] args) {
        Json_Gson_run();
    }
    public static void Json_Gson_run(){
        //创建 列表 name类型
        List<Name> list=new ArrayList<>();
        list.add(new Name("leo","lee","12@qq.com"));
        list.add(new Name("tom","tt","1234@qq.com"));
        //这其实也是列表
        JsonArray array=new JsonArray();
        //linkedtreemap
        JsonObject obj=new JsonObject();
        for(Name n:list){
            obj.addProperty("firstName",n.getFirstName());
            obj.addProperty("lastName",n.getLastName());
            obj.addProperty("email",n.getEmail());
            array.add(obj);
        }
        Writer w= null;
        try {
            System.out.println("ss");
            File f=new File("E://idea-workspace/java_lesson/src/第十七章_XML与JSON/JSON_GSON通用版/O.json");
            f.createNewFile();
            w = new FileWriter(f);
            w.write(array.toString());
            w.close();
            System.out.println("over!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(array.toString());

    }

}
